package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.ToastUtils
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.adapter.DeviceDetailAdapter
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.*
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.SimpleOnRefreshLoadMoreListener
import com.jjc.android.util.paramEx
import com.lzy.okgo.OkGo
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.greenrobot.eventbus.EventBus

class DeviceDetailActivity : BaseActivity(){

    companion object{
        fun start(context: Context,id:String){
            val intent = Intent(context,DeviceDetailActivity::class.java)
            intent.putExtra(Constants.BUNDLE_ID,id)
            context.startActivity(intent)
        }
    }

    override val layoutId: Int = R.layout.activity_operation_video
    private val smartRefreshLayout: SmartRefreshLayout by lazy { findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout) }
    private val recyclerView: RecyclerView by lazy { findViewById<RecyclerView>(R.id.recyclerView) }
    private lateinit var mAdapter:DeviceDetailAdapter
    private var deviceId:String = ""
    private val refreshLayoutDelegate by lazy {
        object : SimpleOnRefreshLoadMoreListener(){
            override fun invokeActivity(block: BaseActivity.() -> Unit) {
                this@DeviceDetailActivity.apply(block)
            }

            override fun loadPage(
                page: Int,
                actionSuccess: (Int, List<Any>?) -> Unit,
                actionError: (Int, String?) -> Unit
            ) {
                OkGo.post<LzyResponse<PageInfo<DeviceDto>>>(Constants.getFormList)
                    .paramEx(mutableMapOf("page" to page.toString(),"pagesize" to Constants.PAGE_SIZE.toString(),"needpage" to "1","formheaduuid" to "2582bc4c14914246807a26886f46dd0e","filters" to listOf<KeyValuePair>(
                        KeyValuePair("bhmohipntdlpc61m",deviceId)
                    )),true)
                    .tag(this)
                    .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<DeviceDto>>>(false){
                        override fun onCallSuccess(response: LzyResponse<PageInfo<DeviceDto>>) {
                            actionSuccess.invoke(page,response.data?.data)
                        }

                        override fun onCallError(error: String?) {
                            super.onCallError(error)
                            actionError.invoke(page,error)
                        }
                    })
            }
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.auth_account_txt))
        recyclerView.layoutManager = LinearLayoutManager(this)
        mAdapter = DeviceDetailAdapter()
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val info = mAdapter.getItem(position)?:return@setOnItemChildClickListener
            when(view.id){
                R.id.unBindBtn->{
                    MessageImageDialog.Builder(this)
                        .title(getString(R.string.unbind_device_txt))
                        .content(getString(R.string.unbind_device_hint_txt))
                        .okClick {
                            OkGo.post<LzyResponse<Any>>(Constants.deleteFormRow)
                                .paramEx(mutableMapOf("rowid" to info.id,"formheaduuid" to "2582bc4c14914246807a26886f46dd0e"),true)
                                .tag(this)
                                .execute(object : SimpleHttpCallback<LzyResponse<Any>>(activity = this@DeviceDetailActivity){
                                    override fun onCallSuccess(response: LzyResponse<Any>) {
                                        EventBus.getDefault().post(ChangeDeviceEvent())
                                        ToastUtils.show(getString(R.string.unbind_success_txt))
                                        refreshLayoutDelegate.requestRefresh(false)
                                    }
                                })
                        }
                        .build()
                        .show()
                }
            }

        }
        recyclerView.adapter = mAdapter
        mAdapter.addHeaderView(layoutInflater.inflate(R.layout.item_device_detail_header,recyclerView,false))
        smartRefreshLayout.setEnableLoadMore(false)
        refreshLayoutDelegate.init(smartRefreshLayout,recyclerView,false)
    }

    override fun setupArguments(args: Bundle?) {
        deviceId = args?.getString(Constants.BUNDLE_ID)?:""
    }
}